package langchain4j.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import langchain4j.entity.User;
import langchain4j.entity.dto.PasswordUpdateDTO;
import langchain4j.mapper.UserMapper;
import langchain4j.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean updateUser(Long id, User user) {
        if (id == null) {
            return false;
        }
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId, id)
                .eq(User::getIsDelete, 0);

        return userMapper.update(user, wrapper) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePassword(Long id, PasswordUpdateDTO dto) {
        // 1. 查询用户（使用基本Wrapper确保可靠性）
        User user = userMapper.selectOne(new QueryWrapper<User>()
                .eq("id", id)
                .select("id", "password"));

        if (user == null) {
            return false;
        }

        // 2. 验证当前密码（明文对比）
        if (!Objects.equals(dto.getCurrentPassword(), user.getPassword())) {
            return false;
        }

        // 3. 检查新旧密码是否相同
        if (dto.getNewPassword().equals(user.getPassword())) {
            return false;
        }

        // 4. 更新密码
        User updateUser = new User();
        updateUser.setId(id);
        updateUser.setPassword(dto.getNewPassword());
        return userMapper.updateById(updateUser) > 0;
    }

    @Override
    public User findUserByIdCardAndPassword(String idCard, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getIdCard,idCard);
        queryWrapper.eq(User::getPassword,password);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean register(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getIdCard,user.getIdCard());
        queryWrapper.eq(User::getPhone,user.getPhone());
        User req = userMapper.selectOne(queryWrapper);
        if(req != null){
            return false;
        }else {
            userMapper.register(user);
            return true;
        }
    }

    @Override
    public boolean resetPass(User user) {
        // 1. 参数校验
        if (StringUtils.isBlank(user.getPhone())
                || StringUtils.isBlank(user.getIdCard())
                || StringUtils.isBlank(user.getPassword())) {
            throw new IllegalArgumentException("手机号、身份证号和新密码不能为空");
        }

        // 2. 验证用户是否存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, user.getPhone())
                .eq(User::getIdCard, user.getIdCard());
        if (userMapper.selectCount(queryWrapper) == 0) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户不存在或信息不匹配");
        }

        // 3. 执行密码更新
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getPhone, user.getPhone())
                .eq(User::getIdCard, user.getIdCard())
                .set(User::getPassword, user.getPassword());
        return userMapper.update(null, updateWrapper) > 0;
    }
}